package VIEWS;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import CONTROLLERS.NhanVienControllers;
import ModelEntityFW.NhanVienEntity;
import ModelEntityFW.UserEntitys;
import Ultils.DateConverter;

import javax.swing.JToolBar;
import javax.swing.JButton;
import java.awt.Font;

import javax.naming.Binding;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import java.awt.event.ActionEvent;
import com.toedter.calendar.JDateChooser;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class AddNhanVien extends JFrame {

	private JPanel contentPane;
	private static Connection m_objConnection;
	private static UserEntitys m_objSecurityContext;
	private NhanVienControllers m_NhanVienControllers;
	private NhanVienEntity m_NhanVienEntity;
	private JTextField hoVaTen;
	private JTextField diaChi;
	private JTextField phone;
	private JButton button;
	private JDateChooser ngaySinh;
	private JDateChooser ngayLViec;
	private JComboBox trinhDo;
	private JComboBox chucVu;
	private JComboBox tenKhoa;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the frame.
	 */
	public AddNhanVien(Connection objConnection, UserEntitys objSecurity, NhanVienEntity objNhanVienEntity) {
		
		m_objConnection = objConnection;
		m_objSecurityContext = objSecurity;
		m_NhanVienEntity = objNhanVienEntity;
		m_NhanVienControllers = new NhanVienControllers(m_objConnection, m_objSecurityContext);
		
		button = new JButton("Button");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				m_NhanVienEntity.setHoVaTen(hoVaTen.getText());
				m_NhanVienEntity.setDiaChi(diaChi.getText());
				m_NhanVienEntity.setPhone(phone.getText());
				m_NhanVienEntity.setTrinhDo(""+trinhDo.getSelectedItem());
				m_NhanVienEntity.setNgaySinh(DateConverter.convertDateToString(ngaySinh.getDate(), "yyyy-MM-dd"));
				m_NhanVienEntity.setNgayLamViec(DateConverter.convertDateToString(ngayLViec.getDate(), "yyyy-MM-dd"));
				m_NhanVienEntity.setMaKhoa(tenKhoa.getSelectedIndex()+2);
				m_NhanVienEntity.setMaChucVu(chucVu.getSelectedIndex()+1);
				if(m_NhanVienEntity.isbFlag()){
					
					boolean bResult = false;
					try {
						bResult = m_NhanVienControllers.ExecOfUpdateElement(m_NhanVienEntity);
						
					} catch (Exception e) {
						JOptionPane.showMessageDialog(getParent(), "Err: "+e.getMessage());
						// TODO: handle exception
					}
					if(bResult){
						JOptionPane.showMessageDialog(getParent(), "Đã Cập Nhật!!");
					}
				}else{
					
					boolean bResult = false;
					try {
						bResult = m_NhanVienControllers.ExecOfInsertElement(m_NhanVienEntity);
						
					} catch (Exception e) {
						JOptionPane.showMessageDialog(getParent(), "Err: "+e.getMessage());
						// TODO: handle exception
					}
					if(bResult){
						JOptionPane.showMessageDialog(getParent(), "Thêm Mới Thành Công!!");
					}
				}
				
				dispose();
			}
		});
		if(m_NhanVienEntity.isbFlag()){
			button.setText("Cập nhật");
			
			
		}else{
			button.setText("Thêm mới");
		}
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 701, 485);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JToolBar toolBar = new JToolBar();
		toolBar.setBounds(10, 22, 665, 43);
		contentPane.add(toolBar);
		
		
		button.setIcon(new ImageIcon(AddNhanVien.class.getResource("/images/book_edit.png")));
		button.setFont(new Font("Cambria", Font.PLAIN, 16));
		toolBar.add(button);
		
		JButton btnDong = new JButton("Đóng[x]");
		btnDong.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnDong.setIcon(new ImageIcon(AddNhanVien.class.getResource("/images/6_3.png")));
		btnDong.setFont(new Font("Cambria", Font.PLAIN, 16));
		toolBar.add(btnDong);
		
		JLabel lblHoVaTen = new JLabel("Họ Và Tên:");
		lblHoVaTen.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblHoVaTen.setBounds(10, 98, 84, 29);
		contentPane.add(lblHoVaTen);
		
		JLabel lblDiaChi = new JLabel("Địa Chỉ:");
		lblDiaChi.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblDiaChi.setBounds(10, 164, 97, 32);
		contentPane.add(lblDiaChi);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblPhone.setBounds(10, 235, 46, 32);
		contentPane.add(lblPhone);
		
		JLabel lblNgaySinh = new JLabel("Ngày Sinh:");
		lblNgaySinh.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNgaySinh.setBounds(10, 383, 84, 32);
		contentPane.add(lblNgaySinh);
		
		JLabel lblNgayLamViec = new JLabel("Ngày Làm Việc:");
		lblNgayLamViec.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNgayLamViec.setBounds(310, 383, 115, 32);
		contentPane.add(lblNgayLamViec);
		
		hoVaTen = new JTextField();
		hoVaTen.setBounds(131, 97, 244, 32);
		contentPane.add(hoVaTen);
		hoVaTen.setColumns(10);
		
		diaChi = new JTextField();
		diaChi.setBounds(131, 165, 502, 32);
		contentPane.add(diaChi);
		diaChi.setColumns(10);
		
		phone = new JTextField();
		phone.setBounds(131, 236, 136, 32);
		contentPane.add(phone);
		phone.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Trình Độ:");
		lblNewLabel_1.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNewLabel_1.setBounds(310, 235, 84, 33);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("Chức Vụ:");
		lblNewLabel_2.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNewLabel_2.setBounds(10, 307, 97, 29);
		contentPane.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("Tên Khoa:");
		lblNewLabel_3.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNewLabel_3.setBounds(310, 307, 65, 29);
		contentPane.add(lblNewLabel_3);
		
		ngaySinh = new JDateChooser();
		ngaySinh.setDateFormatString("dd/MM/yyyy");
		ngaySinh.setBounds(133, 383, 136, 32);
		contentPane.add(ngaySinh);
		
		ngayLViec = new JDateChooser();
		ngayLViec.setDateFormatString("dd/MM/yyyy");
		ngayLViec.setBounds(429, 383, 136, 32);
		contentPane.add(ngayLViec);
		
		trinhDo = new JComboBox();
		trinhDo.setFont(new Font("Cambria", Font.PLAIN, 14));
		trinhDo.setModel(new DefaultComboBoxModel(new String[] {"Giáo sư", "Tiến sĩ", "Thạc sĩ", "Đại học", "Cao đẳng"}));
		trinhDo.setBounds(429, 236, 204, 32);
		contentPane.add(trinhDo);
		
		chucVu = new JComboBox(getItemChucVu(m_objConnection));
		chucVu.setFont(new Font("Cambria", Font.PLAIN, 14));
		chucVu.setBounds(131, 304, 136, 32);
		contentPane.add(chucVu);
		
		tenKhoa = new JComboBox(getItemKhoa(m_objConnection));
		tenKhoa.setFont(new Font("Cambria", Font.PLAIN, 14));
		tenKhoa.setBounds(429, 304, 136, 32);
		contentPane.add(tenKhoa);
		if(m_NhanVienEntity.isbFlag()){
			BindingControl(m_NhanVienEntity);
		}
	}
	
	public static Vector<String> getItemChucVu(Connection objConnection){
		CallableStatement objCsmt = null;
		ResultSet objRs = null;
		Vector<String> item = new Vector<String>();
		try {
			if(!objConnection.isClosed()){
				objCsmt = objConnection.prepareCall("{CALL SP_GET_TEN_CHUCVU()}");
				objRs = objCsmt.executeQuery();
			}
			while(objRs.next()){
				item.addElement(objRs.getString("TenChucVu"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.getMessage();
		}
		return item;
	}
	
	public static Vector<String> getItemKhoa(Connection objConnection){
		CallableStatement objCsmt = null;
		ResultSet objRs = null;
		Vector<String> item = new Vector<String>();
		try {
			if(!objConnection.isClosed()){
				objCsmt = objConnection.prepareCall("{CALL SP_GET_TEN_KHOA()}");
				objRs = objCsmt.executeQuery();
			}
			while(objRs.next()){
				item.addElement(objRs.getString("TenKhoa"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.getMessage();
		}
		return item;
	}
	
	public void BindingControl(NhanVienEntity objNhanVien){
		
		hoVaTen.setText(objNhanVien.getHoVaTen());
		diaChi.setText(objNhanVien.getDiaChi());
		phone.setText(objNhanVien.getPhone());
		trinhDo.setSelectedItem(""+objNhanVien.getTrinhDo());
		ngaySinh.setDate(DateConverter.convertStringToDate(objNhanVien.getNgaySinh(), "yyyy-MM-dd"));
		ngayLViec.setDate(DateConverter.convertStringToDate(objNhanVien.getNgayLamViec(), "yyyy-MM-dd"));
		tenKhoa.setSelectedIndex(objNhanVien.getMaKhoa());
		chucVu.setSelectedIndex(objNhanVien.getMaChucVu());
	}
}
